import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;
import java.io.*;

public class Test {
	public static void main(String[] args) throws Exception {
		FileReader groupFileR = new FileReader("LLVMTemplate.stg");
		StringTemplateGroup templates =
			new StringTemplateGroup(groupFileR);
		groupFileR.close();

		ANTLRInputStream input = new ANTLRInputStream(System.in);
		CLexer lexer = new CLexer(input);

		CommonTokenStream tokens = new CommonTokenStream(lexer);
		CParser parser = new CParser(tokens);
		CParser.translation_unit_return r = parser.translation_unit();

		Tree t = (Tree)r.getTree();


		//System.out.println(t.toStringTree());

		// CommonTreeAdaptor adaptor = new CommonTreeAdaptor();
		// CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, t);
		CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
		//System.out.println(nodes.toTokenTypeString());
		nodes.setTokenStream(tokens);
		//nodes.reset();

		// System.out.println(t.getChild(0).getChild(1).toString());
		// System.out.println(t.getChild(1).getChild(1).toString());
		// TestTreeIterator it = new TestTreeIterator(t);
		// while (it.hasNext())
		// {
		// 	Tree o = (Tree)it.next();
		// 	System.out.println(o.toString() + " " + o.getChildCount());
		// }

		// while(true) {
	 //      Token token = ((CommonTree)nodes.nextElement()).getToken();
	 //      if(token.getType() == CParser.EOF) break;
	 //      System.out.printf("%-10s '%s'\n", CParser.tokenNames[token.getType()], token.getText());
	 //    }

		CSyntaxTree walker = new CSyntaxTree(nodes);
		walker.setTemplateLib(templates);

		CSyntaxTree.translation_unit_return r2 = walker.translation_unit();
		StringTemplate output = (StringTemplate)r2.getTemplate();
		System.out.println(output.toString());
	}
}
